package com.linxiao.hrcrm.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.linxiao.hrcrm.domain.CrmCustomerRelationship;
import com.linxiao.hrcrm.service.ICrmCustomerRelationshipService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.linxiao.hrcrm.mapper.CrmFollowUpMapper;
import com.linxiao.hrcrm.domain.CrmFollowUp;
import com.linxiao.hrcrm.service.ICrmFollowUpService;

import javax.annotation.Resource;

/**
 * 跟进记录Service业务层处理
 *
 * @author zhoudong
 * @date 2024-03-13
 */
@Service
public class CrmFollowUpServiceImpl implements ICrmFollowUpService {
    @Resource
    private CrmFollowUpMapper crmFollowUpMapper;
    @Resource
    private ICrmCustomerRelationshipService relationshipService;

    /**
     * 查询跟进记录
     *
     * @param id 跟进记录主键
     * @return 跟进记录
     */
    @Override
    public CrmFollowUp selectCrmFollowUpById(Long id) {
        return crmFollowUpMapper.selectCrmFollowUpById(id);
    }

    /**
     * 查询跟进记录列表
     *
     * @param crmFollowUp 跟进记录
     * @return 跟进记录
     */
    @Override
    public List<CrmFollowUp> selectCrmFollowUpList(CrmFollowUp crmFollowUp) {
        return crmFollowUpMapper.selectCrmFollowUpList(crmFollowUp);
    }

    /**
     * 新增跟进记录
     *
     * @param crmFollowUp 跟进记录
     * @return 结果
     */
    @Override
    public int insertCrmFollowUp(CrmFollowUp crmFollowUp) {
        CrmCustomerRelationship query = new CrmCustomerRelationship();
        query.setEmployeeId(crmFollowUp.getEmployeeId());
        query.setCustomerId(crmFollowUp.getCustomerId());
        List<CrmCustomerRelationship> crmCustomerRelationships = relationshipService.selectCrmCustomerRelationshipList(query, Boolean.FALSE);

        // 更新客户跟进状态，如果是未联系，改为初访
        if (CollUtil.isNotEmpty(crmCustomerRelationships)) {
            for (CrmCustomerRelationship relationship : crmCustomerRelationships) {
                if (StrUtil.isBlank(relationship.getCustomerState())
                        || CrmCustomerRelationship.CUSTOMER_STATE_WEILIANXI.equals(relationship.getCustomerState())) {
                    relationship.setCustomerState(CrmCustomerRelationship.CUSTOMER_STATE_CHUFANG);
                    relationshipService.updateCrmCustomerRelationship(relationship);
                }
            }
        }

        crmFollowUp.setCreateTime(DateUtils.getNowDate());
        return crmFollowUpMapper.insertCrmFollowUp(crmFollowUp);
    }

    /**
     * 修改跟进记录
     *
     * @param crmFollowUp 跟进记录
     * @return 结果
     */
    @Override
    public int updateCrmFollowUp(CrmFollowUp crmFollowUp) {
        crmFollowUp.setUpdateTime(DateUtils.getNowDate());
        return crmFollowUpMapper.updateCrmFollowUp(crmFollowUp);
    }

    /**
     * 批量删除跟进记录
     *
     * @param ids 需要删除的跟进记录主键
     * @return 结果
     */
    @Override
    public int deleteCrmFollowUpByIds(Long[] ids) {
        return crmFollowUpMapper.deleteCrmFollowUpByIds(ids);
    }

    /**
     * 删除跟进记录信息
     *
     * @param id 跟进记录主键
     * @return 结果
     */
    @Override
    public int deleteCrmFollowUpById(Long id) {
        return crmFollowUpMapper.deleteCrmFollowUpById(id);
    }
}
